﻿using System;
using System.Threading;
using Netbits.Common.Threading;
namespace Netbits.Common.Collections
{
	/// <summary>
	/// Interface for ICollection Event-Handling
	/// </summary>
	/// <typeparam name="TItem">Type of the items</typeparam>
	/// <typeparam name="TSender">Type of the sender.</typeparam>
	public interface ICollectionEvents<TSender, TItem>
	{
		/// <summary>
		/// Occurs before an item is added to list
		/// </summary>
		SaveEventHandler<TSender, TItem> BeforeAddItem { get; }
		/// <summary>
		/// Occurs after an item was added to list
		/// </summary>
		SaveEventHandler<TSender, TItem> AfterAddItem { get; }
		/// <summary>
		/// Occurs before an item is insert on specified index-position.
		/// </summary>
		SaveEventHandler<TSender, int, TItem> BeforeInsertItem { get; }
		/// <summary>
		/// Occurs after an items was insert at specified index-position
		/// </summary>
		SaveEventHandler<TSender, int, TItem> AfterInsertItem { get; }
		/// <summary>
		/// Occurs before an item was removed from the list
		/// </summary>
		SaveEventHandler<TSender, TItem> BeforeRemoveItem { get; }
		/// <summary>
		/// Occurs after an item was removed from list.
		/// </summary>
		SaveEventHandler<TSender, TItem> AfterRemoveItem { get; }
		/// <summary>
		/// Occurs before an item was removed from the list at specified index.
		/// </summary>
		SaveEventHandler<TSender, int, TItem> BeforeRemoveItemAt { get; }
		/// <summary>
		/// Occurs after an item was successfully removed from the list at the specified index
		/// </summary>
		SaveEventHandler<TSender, int, TItem> AfterRemoveItemAt { get; }
		/// <summary>
		/// Occurs before an item was modified 
		/// </summary>
		SaveEventHandler<TSender, TItem> BeforeModifyItem { get; }
		/// <summary>
		/// Occurs after an item was modified
		/// </summary>
		SaveEventHandler<TSender, TItem> AfterModifyItem { get; }
		/// <summary>
		/// Occurs before the list was cleared
		/// </summary>
		SaveEventHandler<TSender> BeforeClearItems { get; }
		/// <summary>
		/// Occurs after the list is cleared
		/// </summary>
		SaveEventHandler<TSender> AfterClearItems { get; }

		/// <summary>
		/// Defines if the events are fired asynchronous
		/// </summary>
		bool UseAsyncEvents
		{
			get;
			set;
		}

		/// <summary>
		/// After this function is called the calling thread was blocked until any item is in the list
		/// </summary>
		void WaitForItems();
		/// <summary>
		/// After this function is called the calling thread was blocked until any item is in the list or the specified timeout was reached
		/// </summary>
		/// <param name="Timeout_">The specified timeout to wait</param>
		/// <returns>true if new items are available, otherwise false</returns>
		bool WaitForItems( int Timeout_ );
		/// <summary>
		/// Get the Wait-Handler to wait for items 
		/// </summary>
		/// <returns>The ReaderWriterLockSlim</returns>
		ManualResetEventSlim WaitHandler();
	}
}
